package JET.CenterPanels;

import JET.Centers.Scripts.MyScript;
import JET.mygui.CenterTabsPane;
import JET.mygui.CenterTabsPane.CenterTabsPaneTab;
import JET.mygui.CenterTabsPane.TabType;
import JET.mygui.TabsPane;
import JET.mygui.TabsPane.TabListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JTextArea;

/*
 * CPScriptEditor.java
 *
 * Created on 2010-08-28, 13:38:00
 */
/**
 *
 * @author 057Ry
 */
public class CPScriptEditor extends javax.swing.JPanel implements CenterTabsPaneTab{

    MyScript script;

    /** Creates new form CPScriptEditor */
    public CPScriptEditor() {
        initComponents();

        System.out.println("tabs size: "+textArea.getTabSize());
        textArea.setTabSize(0);

        final JTextArea ta = this.textArea;
        textArea.addKeyListener( new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_TAB)
                    ta.insert("    ", ta.getCaretPosition());
            }
        });
    }

    public TabType getTabType() {
        return CenterTabsPane.TabType.JavaScriptEditor;
    }
    public String getTitle() {
        return script.getName();
    }

    public void setScript(MyScript script) {
        this.script = script;
        textArea.setText(script.getScript());
        getAccessibleContext().setAccessibleName(script.getName());
    }

    public void saveScript() {
        script.setScript(textArea.getText());
    }

    ////////////////////////////////////////////////////////////////////////////
    //             T A B   S E L E C T I O N   L I S T E N E R S              //
    ////////////////////////////////////////////////////////////////////////////

    ArrayList<TabListener> tabListeners = new ArrayList<TabListener>();

    public void addTabSelectionListener(TabListener l) {
        if (!tabListeners.contains(l))
            tabListeners.add(l);
    }
    public void removeTabSelectionListener(TabListener l) {
        tabListeners.remove(l);
    }
    public void notifyTabSelected(TabsPane tp) {
        for(TabListener l : tabListeners)
            l.tabSelected(tp);
    }
    public void notifyTabFocused(TabsPane tp) {
        for(TabListener l : tabListeners)
            l.tabFocused(tp);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        textArea.setColumns(20);
        textArea.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        textArea.setRows(5);
        jScrollPane1.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables

}
